<template>
  <div>
    <div class="amap-page-container2">
      <el-amap
        vid="amapDemo"
        ref="map"
        :center="center"
        :zoom="zoom"
        :events="events"
      ></el-amap>
    </div>
  </div>
</template>
<script>
var placeSearch
var cpoint
module.exports = {
  data () {
    let self = this
    return {
      zoom: 12,
      center: [116.413449, 40.050802],
      inputvalue: '',
      flag: '',
      pathdata1: this.$route.query.pathdata,
      allinfo: this.$route.query.allinfo,
      events: {
        init: (o) => {
          AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function (PathSimplifier, $) {
            if (!PathSimplifier.supportCanvas) {
              alert('当前环境不支持 Canvas！')
              return
            }
            var pathSimplifierIns = new PathSimplifier({
              zIndex: 100,
              autoSetFitView: true,
              map: o,
              getPath: function (pathData, pathIndex) {
                return pathData.path
              },
              getHoverTitle: function (pathData, pathIndex, pointIndex) {
                if (pointIndex >= 0) {
                  return pathData.name + '，点：' + (pointIndex + 1) + '/' + pathData.path.length
                }
                return pathData.name + '，点数量' + pathData.path.length
              },
              renderOptions: {
                renderAllPointsIfNumberBelow: 100,
                keyPointOnSelectedPathLineStyle: {
                  radius: 10,
                  fillStyle: '#087ec4'
                },
                startPointStyle: {
                  radius: 10,
                  fillStyle: '#087ec4',
                  strokeStyle: 'grey'
                }
              }
            })
            window.pathSimplifierIns = pathSimplifierIns
            pathSimplifierIns.setData([{
              name: '轨迹',
              path: self.pathdata1
            }])
            pathSimplifierIns.on('pointClick', function (e, info) {
              var geocoder = new AMap.Geocoder()
              //console.log(info.pathData.path[info.pointIndex])
              AMapUI.loadUI(['overlay/SimpleInfoWindow'], function (SimpleInfoWindow) {
                geocoder.getAddress(info.pathData.path[info.pointIndex], function (status, result) {
                  if (status === 'complete' && result.regeocode) {
                    let address = result.regeocode.formattedAddress
                    let infoWindow1 = new SimpleInfoWindow({
                      infoTitle: '<b>详细信息</b>',
                      infoBody: '抵达地址:' + address + '<br>抵达时间:' + self.allinfo[info.pointIndex].dBegin + '<br>离开时间:' + self.allinfo[info.pointIndex].dEnd + '<br>停留时间:' + self.allinfo[info.pointIndex].stay,
                      offset: new AMap.Pixel(0, -31)
                    })
                    infoWindow1.open(o, info.pathData.path[info.pointIndex])
                  } else {
                    alert('根据经纬度查询地址失败')
                  }
                })
              })
            })
          })
        }
      }
    }
  },
  methods: {
    onInputdone () {
      //console.log(this.inputvalue)
      placeSearch.searchNearBy(this.inputvalue, cpoint, 200, function (status, result) {

      })
    },
    closeMap () {
      //console.log(this.flag + '关了')
      this.$router.go(-1)
    },
    sure () {
      // 回写
      // this.$toast(item.id + '-->' + item.title)
      this.flag = '回写'
      this.$router.go(-1)
    },
    change (t) {
      if (t < 10) {
        return '0' + t
      } else {
        return t
      }
    },
    currenttime () {
      let d = new Date()
      let year = d.getFullYear()
      let month = this.change(d.getMonth() + 1)
      let day = this.change(d.getDate())
      let hour = this.change(d.getHours())
      let minute = this.change(d.getMinutes())
      let second = this.change(d.getSeconds())
      let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
      return time
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === 'new' || to.name === 'edit' || to.name === 'qiandaonew') {
      if (this.flag) {
        for (let groupindex in this.sval) {
          for (let itemindex in this.sval[groupindex].groupvalue) {
            let item = this.sval[groupindex].groupvalue[itemindex]
            if (item.sField === this.svalmap.sField) {
              //console.log(item.value + 'map')
              alert('位置:' + this.mapaddressposition.split('&')[0])
              item.value = this.mapaddressposition
              this.isPosi = false
            } else if (item.sField === 'dbegin' && this.svalmap.sField === 'baddress') {
              item.value = this.currenttime()
            } else if (item.sField === 'dend' && this.svalmap.sField === 'eaddress') {
              item.value = this.currenttime()
            }
          }
        }
      }
    }
    next()
  }
}
</script>
<style>
.amap-page-container2 {
  height: 100vh;
  width: 100vw;
}
.leftfloat {
  position:absolute;
  left: 1vw;
  top: 1vh;
  z-index: 99;
}
.rightfloat {
  position:absolute;
  right: 1vw;
  top: 1vh;
  z-index: 99;
}
</style>
